class Solution {
public:
int produce_result(int A[], int begin, int end){
    int temp = begin, result = 1;
    if(begin == end){
        return 0;
    }
    if(begin == end-1){
        return A[begin];
    }
    while(temp < end){
        result *= A[temp++];
    }
	return result;
}
int maxprod(int A[], int begin, int end){
	if(begin == end-1){
        return A[begin];
    }else if(begin == end){
        return 0;
    }
    int left_neg = begin, right_neg = end-1;
    while(left_neg < end && A[left_neg] > 0){
        left_neg++;
    }
    while(right_neg > begin-1 && A[right_neg] > 0){
        right_neg--;
    }
    int result = produce_result(A, begin, end);
    if(left_neg == end){
        return result;
    }
    if(right_neg == begin-1){
        return result;
    }
    if(result > 0){
        return result;
    }else{

        int left_result = produce_result(A, begin, right_neg);
        int right_result = produce_result(A, left_neg+1, end);
        return (left_result > right_result) ? left_result : right_result;
    }
}
int maxProduct(int A[], int n) {
        int begin = 0, end = 0, max, iszero = 0;
        int lh, rh;
		if(n==0){
			return 0;
		}
        if(n==1){
            return A[begin];
        }
        while(end < n){
            while(end < n && A[end] != 0){
                end++;
            }
            int temp = maxprod(A, begin, end);
            if(begin == 0){
				max = temp;
			}
            if(end < n && A[end] == 0){
                iszero = 1;
            }
			
            if(temp > max){
                max = temp;
            }
            begin = end+1;
            end++;
        }
        if(iszero){
            return (max > 0) ? max : 0;
        }else{
            return max;
        }
}
   
};
